package com.LachesismStorm.lachesism.core.util;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;

public class SendMessage {
    private static final String ACCESS_KEY_ID = "LTAI5t6Ue1WGFbpRhzY8efjm";
    private static final String ACCESS_KEY_SECRET = "KQ2ruYvoWLd6qJFsHUv9xYsswKcBO7";
    private static final String REGION_ID = "cn-shanghai";
    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";


    public static void sendMessage(String code, String phone) throws Exception {
        AsyncClient client = createClient();
        SendSmsRequest sendSmsRequest = buildSendSmsRequest("SMS_465730629", "{\"code\":\"" + code + "\"}", "Lachesism", phone);
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        client.close();
    }

    private static AsyncClient createClient() {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(ACCESS_KEY_ID)
                .accessKeySecret(ACCESS_KEY_SECRET)
                .build());
        return AsyncClient.builder()
                .region(REGION_ID)
                .credentialsProvider(provider)
                .overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride(ENDPOINT))
                .build();
    }

    private static SendSmsRequest buildSendSmsRequest(String templateCode, String templateParam, String signName, String phone) {
        return SendSmsRequest.builder()
                .templateCode(templateCode)
                .templateParam(templateParam)
                .signName(signName)
                .phoneNumbers(phone)
                .build();
    }
    public static void sendMessage2(String phone) throws Exception {
        AsyncClient client = createClient();
        SendSmsRequest sendSmsRequest = buildSendSmsRequest("SMS_465965228", null, "Lachesism", phone);
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse sendSmsResponse = response.get();
        System.out.println(sendSmsResponse);
    }

}
